<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="接收单位" prop="companyId">
      <el-input v-model="dataForm.companyId" placeholder="接收单位"></el-input>
    </el-form-item>
    <el-form-item label="账期" prop="billDate">
      <el-input v-model="dataForm.billDate" placeholder="账期"></el-input>
    </el-form-item>
    <el-form-item label="结算状态0未结算1已结算" prop="status">
      <el-input v-model="dataForm.status" placeholder="结算状态0未结算1已结算"></el-input>
    </el-form-item>
    <el-form-item label="运费" prop="shipmentAmount">
      <el-input v-model="dataForm.shipmentAmount" placeholder="运费"></el-input>
    </el-form-item>
    <el-form-item label="运输次数" prop="shipmentCount">
      <el-input v-model="dataForm.shipmentCount" placeholder="运输次数"></el-input>
    </el-form-item>
    <el-form-item label="吨数" prop="weight">
      <el-input v-model="dataForm.weight" placeholder="吨数"></el-input>
    </el-form-item>
    <el-form-item label="出票日期" prop="ticketDate">
      <el-input v-model="dataForm.ticketDate" placeholder="出票日期"></el-input>
    </el-form-item>
    <el-form-item label="出票人" prop="ticketMaker">
      <el-input v-model="dataForm.ticketMaker" placeholder="出票人"></el-input>
    </el-form-item>
    <el-form-item label="收票人" prop="ticketCollector">
      <el-input v-model="dataForm.ticketCollector" placeholder="收票人"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="note">
      <el-input v-model="dataForm.note" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="结算时间" prop="settlementTime">
      <el-input v-model="dataForm.settlementTime" placeholder="结算时间"></el-input>
    </el-form-item>
    <el-form-item label="结算人员" prop="settlementUser">
      <el-input v-model="dataForm.settlementUser" placeholder="结算人员"></el-input>
    </el-form-item>
    <el-form-item label="结算人员id" prop="settlementUserId">
      <el-input v-model="dataForm.settlementUserId" placeholder="结算人员id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: undefined,
          companyId: '',
          billDate: '',
          status: '',
          shipmentAmount: '',
          shipmentCount: '',
          weight: '',
          ticketDate: '',
          ticketMaker: '',
          ticketCollector: '',
          note: '',
          settlementTime: '',
          settlementUser: '',
          settlementUserId: ''
        },
        dataRule: {
          companyId: [
            { required: true, message: '接收单位不能为空', trigger: 'blur' }
          ],
          billDate: [
            { required: true, message: '账期不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '结算状态0未结算1已结算不能为空', trigger: 'blur' }
          ],
          shipmentAmount: [
            { required: true, message: '运费不能为空', trigger: 'blur' }
          ],
          shipmentCount: [
            { required: true, message: '运输次数不能为空', trigger: 'blur' }
          ],
          weight: [
            { required: true, message: '吨数不能为空', trigger: 'blur' }
          ],
          ticketDate: [
            { required: true, message: '出票日期不能为空', trigger: 'blur' }
          ],
          ticketMaker: [
            { required: true, message: '出票人不能为空', trigger: 'blur' }
          ],
          ticketCollector: [
            { required: true, message: '收票人不能为空', trigger: 'blur' }
          ],
          note: [
            { required: false, message: '备注不能为空', trigger: 'blur' }
          ],
          settlementTime: [
            { required: true, message: '结算时间不能为空', trigger: 'blur' }
          ],
          settlementUser: [
            { required: true, message: '结算人员不能为空', trigger: 'blur' }
          ],
          settlementUserId: [
            { required: true, message: '结算人员id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || undefined
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/tms/settlementrecord/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.companyId = data.settlementRecord.companyId
                this.dataForm.billDate = data.settlementRecord.billDate
                this.dataForm.status = data.settlementRecord.status
                this.dataForm.shipmentAmount = data.settlementRecord.shipmentAmount
                this.dataForm.shipmentCount = data.settlementRecord.shipmentCount
                this.dataForm.weight = data.settlementRecord.weight
                this.dataForm.ticketDate = data.settlementRecord.ticketDate
                this.dataForm.ticketMaker = data.settlementRecord.ticketMaker
                this.dataForm.ticketCollector = data.settlementRecord.ticketCollector
                this.dataForm.note = data.settlementRecord.note
                this.dataForm.settlementTime = data.settlementRecord.settlementTime
                this.dataForm.settlementUser = data.settlementRecord.settlementUser
                this.dataForm.settlementUserId = data.settlementRecord.settlementUserId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/tms/settlementrecord/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'companyId': this.dataForm.companyId,
                'billDate': this.dataForm.billDate,
                'status': this.dataForm.status,
                'shipmentAmount': this.dataForm.shipmentAmount,
                'shipmentCount': this.dataForm.shipmentCount,
                'weight': this.dataForm.weight,
                'ticketDate': this.dataForm.ticketDate,
                'ticketMaker': this.dataForm.ticketMaker,
                'ticketCollector': this.dataForm.ticketCollector,
                'note': this.dataForm.note,
                'settlementTime': this.dataForm.settlementTime,
                'settlementUser': this.dataForm.settlementUser,
                'settlementUserId': this.dataForm.settlementUserId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
